package com.briup.estore.controller;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.briup.estore.bean.Address;
import com.briup.estore.bean.Customer;
import com.briup.estore.service.IOrderService;
import com.briup.estore.service.IShopCarService;

@Controller
@RequestMapping("/login")
public class do_login_after {

	@Autowired
	private IShopCarService shopCarService;

	@Autowired
	private IOrderService orderService;

	@RequestMapping("/putShopCar")
	public String putShopCar(int num, BigInteger book_id, HttpSession session) {
		Customer customer = (Customer) session.getAttribute("customer");
		BigInteger customer_id = customer.getId();
		shopCarService.addOrderLineToShopCar(num, book_id, customer_id);
		return "redirect:/index";
	}

	@RequestMapping("/deleteShopCarItem")
	public String deleteShopCarItem(BigInteger customer_id, BigInteger item_id) {
		shopCarService.deleteShopCarItem(customer_id, item_id);
		return "redirect:/login/shopCart";
	}

	@RequestMapping("submit")
	public String submitOrder(HttpServletRequest request, Address address, HttpSession session) {
		String[] itemIds = request.getParameterValues("item_id");
		List<BigInteger> shopCarItemIds = new ArrayList<BigInteger>();
		if (itemIds != null) {
			for (String s : itemIds) {
				shopCarItemIds.add(new BigInteger(s));
			}
		}
		Customer customer = (Customer) session.getAttribute("customer");
		orderService.insertOrderList(shopCarItemIds, address, customer.getId());
		return "redirect:/login/orderList";
	}

	@RequestMapping("/deleteOrderById")
	public String deleteOrderById(BigInteger id) {
		orderService.deleteOrderById(id);
		return "redirect:/login/orderList";
	};
}
